<?php
/**
 * 华道撤单
 */
class Cli_CasehuadaorevokeController extends Ctrl_Cli{
	public function indexAction(){
		while(true){
            $tTime = time();
            $tRunlogdir = APPLICATION_PATH . '/shell/runlog/cli_casehuadaorevoke.run';
            Tool_Fnc::writefile($tRunlogdir , $tTime);

			$this->opt();
			sleep(1);
		}
		exit;
	}
	private function opt(){
        $tCHRMO = new Case_Huadao_RevokeModel;
        $tUCLMO = new User_Case_LogModel;
        $tUCMO = new User_CaseModel;

        $tCHRDatas = $tCHRMO->field('id,content,created,case_id')->where('status = 0')->fList();
        foreach($tCHRDatas as $tRow){
            $tUCLMO->begin();
            /*$tData = array(
                'title' => '撤单',    
                'created' => $tRow['created'],
                'case_id' => $tRow['case_id'],
            );     
            if(!$tUCLMO->insert($tData)){
                $tUCLMO->back(); 
                continue;
            }
             */
            $tData = array(
                'status' => 3, 
                'id' => $tRow['case_id'],
                'is_dishonor' => 2,
                'dishonor_introduce' => $tRow['content'],
                'updated' => time(),
            );
            if(!$tUCMO->update($tData)){
                $tUCLMO->back(); 
                continue;
            }else{
				//发送短信提醒
				$tSMO = new SmsModel;
				$tUMO = new UserModel;
				$tSql = 'select u.phone from '.$tUCMO->table.' uc left join '.$tUMO->table.' u on uc.uid = u.id where uc.id = '.$tRow['case_id'];
				$tPhone = $tUCMO->query($tSql)[0]['phone'];
				$tCritcode = $tUCMO->query($tSql)[0]['critcode'];
				if(!empty($tPhone)){
					$tHCTGMO = new Huadao_Core_T_GrouppolicyinsuredModel;
					$tSTMO = new Sms_TemplateModel;
					$tPolicyno = $tHCTGMO->field('POLICYNO')->where('INSUREDIDNO = "'.$tCritcode.'"')->fRow()['POLICYNO'];
					$tMid = $tSTMO->field('tpl_id')->where('policyno = "'.$tPolicyno.'" and type = 3')->fRow()['tpl_id'];
					$tSMO->caseStatus($tPhone,$tMid);
				}
				
			}

            $tSql = 'update ' . $tCHRMO->table . ' set status = 1 where id = ' . $tRow['id'];
            if(!$tCHRMO->exec($tSql)){
                $tUCLMO->back(); 
                continue;
            }
            $tUCLMO->commit();
        }
	}
}


